package com.ktwlrj.securitycenter.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.convert.ConvertUtil;
import com.ktwlrj.securitycenter.controller.args.SafeCredentialInfoArgs;
import com.ktwlrj.securitycenter.dto.SafeCredentialInfoDto;
import com.ktwlrj.securitycenter.entity.SafeCredentialInfoEntity;
import com.ktwlrj.securitycenter.mapper.SafeCredentialInfoMapper;
import com.ktwlrj.securitycenter.service.SafeCredentialInfoService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 要件详情表(SafeCredentialInfo)业务层实现
 *
 * @author code generation
 * @date 2020-02-24 18:08:13
 * @since JDK1.8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SafeCredentialInfoServiceImpl extends ServiceImpl<SafeCredentialInfoMapper, SafeCredentialInfoEntity>
		implements SafeCredentialInfoService {
	private final SafeCredentialInfoMapper mapper;

	@Override
	public void save(SafeCredentialInfoArgs args) throws RuntimeException {
		SafeCredentialInfoEntity entity = ConvertUtil.map(args, SafeCredentialInfoEntity.class);
		boolean result = super.save(entity);
		if (!result) {
			throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
		}
	}

	@Override
	public void batchSave(List<SafeCredentialInfoArgs> args) throws RuntimeException {
		List<SafeCredentialInfoEntity> list = ConvertUtil.listAtoListB(args, SafeCredentialInfoEntity.class);
		if (list.size() <= 0) {
			return;
		}
		// 物理删除业务id下的要件明细信息
		mapper.deleteByBusiId(list.get(0).getBusiId());
		boolean result = super.saveBatch(list);
		if (!result) {
			throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
		}
	}

	@Override
	public List<SafeCredentialInfoDto> selectListByModel(String busiId) throws RuntimeException {
		QueryWrapper<SafeCredentialInfoEntity> credentialQueryWrapper = new QueryWrapper<>();
		credentialQueryWrapper.lambda().in(SafeCredentialInfoEntity::getBusiId, busiId)
				.orderByAsc(SafeCredentialInfoEntity::getPage);
		List<SafeCredentialInfoEntity> credentialInfoEntityList = mapper.selectList(credentialQueryWrapper);
		List<SafeCredentialInfoDto> credentialInfoList = ConvertUtil.listAtoListB(credentialInfoEntityList,
				SafeCredentialInfoDto.class);
		return credentialInfoList;
	}
}